<?php
/*
 * Author:YangBo Created on 2009-1-22:����05:02:18
 * 
 * TODO:TODO
 * as9
 * AlbumService
 * 
 */
require_once ("./BaseService.php");
require_once ("./interfaces/IAlbumService.php");
require_once ("./dao/Album.php");
require_once ("./util/ServiceUtil.php");
class AlbumService extends BaseService implements IAlbumService {
	protected $album;
	public function __construct() {
		$this->initializeDB ();
		$this->album = new Album ( );
	}
	/**
	 * 创建相册
	 * @param $target [ano,name,description,thumbnail,creator]
	 * @return:success
	 */
	public function create($target) {
		$cno = strtolower ( trim ( $target [0] ) );
		$name = strtolower ( trim ( $target [1] ) );
		$description = strtolower ( trim ( $target [2] ) );
		$creator = strtolower ( trim ( $target [3] ) );
		
		try {
			$this->sql = "SELECT albums.name FROM albums WHERE albums.name='$name'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			$numRows = mysql_num_rows ( $this->result );
			if ($numRows >= 1) {
				return ServiceUtil::getFailureResult ( "failure", "same album name existed!" );
			} else {
				try {
					$this->sql = "INSERT INTO albums(cno,name,description,creator) VALUES('$cno','$name','$description','$creator')";
					$this->result = mysql_query ( $this->sql, $this->_db );
					return ServiceUtil::getSuccessResult ( "success", "create album success!" );
				} catch ( Exception $exc ) {
					return $exc;
				}
			}
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 读取相册
	 * @param $targetID ano
	 * @return:$this->class_
	 */
	public function read($targetID) {
		if ($targetID) {
			$ano = strtolower ( trim ( $targetID ) );
			try {
				$this->sql = "SELECT * FROM albums WHERE ano='$ano'";
				$this->result = mysql_query ( $this->sql, $this->_db );
				
				while ( $row = mysql_fetch_object ( $this->result ) ) {
					$this->album->ano = $row->ano;
					$this->album->cno = $row->cno;
					$this->album->name = $row->name;
					$this->album->description = $row->description;
					$this->album->thumbnail = $row->thumbnail;
					$this->album->created_time = $row->created_time;
					$this->album->creator = $row->creator;
					$this->album->capcity = $row->capcity;
					$this->album->used = $row->used;
					$this->album->browsed = $row->browsed;
					
					$this->ret [] = $this->album;
				}
				return $this->ret;
			} catch ( Exception $exc ) {
				return $exc;
			}
		} else {
			try {
				$this->sql = "SELECT * FROM albums";
				$this->result = mysql_query ( $this->sql, $this->_db );
				$numRows = mysql_num_rows ( $this->result );
				for($i = 0; $i < $numRows; $i ++) {
					$row = mysql_fetch_object ( $this->result );
					$this->album = new Album ( );
					$this->album->ano = $row->ano;
					$this->album->cno = $row->cno;
					$this->album->name = $row->name;
					$this->album->description = $row->description;
					$this->album->thumbnail = $row->thumbnail;
					$this->album->created_time = $row->created_time;
					$this->album->creator = $row->creator;
					$this->album->capcity = $row->capcity;
					$this->album->used = $row->used;
					$this->album->browsed = $row->browsed;
					
					$this->ret [] = $this->album;
				}
				return $this->ret;
			} catch ( Exception $exc ) {
				return $exc;
			}
		}
	
	}
	/**
	 * 更新相册
	 * @param $target [ano,cno,name,description,thumbnail,creator]
	 * @return:success
	 */
	public function update($target) {
		$ano = strtolower ( trim ( $target [0] ) );
		$cno = strtolower ( trim ( $target [1] ) );
		$name = strtolower ( trim ( $target [2] ) );
		$description = strtolower ( trim ( $target [3] ) );
		$thumbnail = strtolower ( trim ( $target [4] ) );
		$creator = strtolower ( trim ( $target [5] ) );
		
		try {
			$this->sql = "UPDATE albums SET cno='$cno',name='$name',description='$description',thumbnail='$thumbnail',creator='$creator' WHERE ano='$ano'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			return ServiceUtil::getSuccessResult ( "success", "update album success!" );
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 删除相册
	 * @param $targetID ano
	 * @return:success
	 */
	public function delete($targetID) {
		$ano = strtolower ( trim ( $targetID ) );
		try {
			$this->sql = "DELETE FROM albums WHERE ano='$ano'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			return ServiceUtil::getSuccessResult ( "success", "delete album success!" );
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	
	public function __destruct() {
	
	}
}
?>
